package Str;

/**
 * @auther wangchenghao
 * @date 2021/6/8
 * 字符串转换整数 (atoi)
 **/
public class test6 {
    public  static  int myAtoi(String s) {
        char a[]=s.toCharArray();
        int index=0;
        for (int i = 0; i <a.length ; i++) {
            if (a[i]!=' '){
                index=i;
                break;
            }
        }
        boolean positive=true;
        if (index<a.length-1){
            if (a[index+1]=='-'){
                positive=false;
            }
        }
        long result=0;

        for (int i = index; i < a.length; i++) {
            if (((int)a[index]<=9) && ((int)a[index]>=1)){
                result=a[i]*10+result;
            }else {
                return (int)result;
            }
        }

        if (positive){
            return (int)result;
        }else {
            return (int) (result=0-result);
        }
    }

    public static void main(String[] args) {
        String s="12134 sdadsad";
        System.out.println("myAtoi(s) = " + myAtoi(s));
    }
}
